import { ApiProperty } from '@nestjs/swagger';

export const SUCCESS_CODE = 200;

/**
 * 响应结构
 * ok 成功
 * fail 失败
 */
export class ResultData<T = any> {
    constructor(code = SUCCESS_CODE, msg?: string, data?: T) {
        this.code = code;
        this.msg = msg || 'ok';
        this.data = data || null;
    }

    @ApiProperty({ type: 'number', default: SUCCESS_CODE })
    code: number;

    @ApiProperty({ type: 'string', default: 'ok' })
    msg?: string;

    data?: T;

    static ok<J = null>(data?: J, msg?: string): ResultData<J> {
        return new ResultData<J>(SUCCESS_CODE, msg || '成功', data);
    }

    static fail<J = null>(code: number, msg?: string, data?: J): ResultData<J> {
        return new ResultData<J>(code || 500, msg || 'fail', data);
    }
}
